package com.juvepoland.jpdroid.adapters;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.List;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.common.ImageLoader;
import com.juvepoland.jpdroid.models.Comment;
import com.juvepoland.jpdroid.models.News;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LazyCommentAdapter extends BaseAdapter {
	
	private Activity activity;
	private List<Comment> data;
	private static LayoutInflater inflater=null;
	public ImageLoader imageLoader;
	AssetManager assetManager;
	private Typeface basicFontTypeFace;
	private Typeface basicItalicFontTypeFace;
	private boolean displayAvatars;
	
	public LazyCommentAdapter(Activity a, List<Comment> list, Typeface basic, Typeface italic, boolean displayAvatars) {
        activity = a;
        basicFontTypeFace = basic;
        basicItalicFontTypeFace = italic;
        data=list;
        this.displayAvatars = displayAvatars;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.comment_row, null);
        	
        Comment comment = data.get(position);
        
        TextView contentTextView = (TextView)vi.findViewById(R.id.comment_content);
        TextView infoTextView = (TextView)vi.findViewById(R.id.comment_info);
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.comment_image);
        TextView id = (TextView) vi.findViewById(R.id.cid);
        
        comment.setContent(comment.getContent().replaceAll("(\r\n|\n)", "<br />"));
        contentTextView.setText(Html.fromHtml(comment.getContent()));
        contentTextView.setSingleLine(false);
        
        contentTextView.setTypeface(basicItalicFontTypeFace);
        infoTextView.setTypeface(basicFontTypeFace);
        
        
        
        id.setText(String.valueOf(comment.getId()));
        Format formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        infoTextView.setText(comment.getUser().getUsername()+" || "+formatter.format(comment.getDate()));
        
        if(displayAvatars){
        	if(comment.getUser().getAvatarSource().length()>0){
            	imageLoader.DisplayImage("http://forum.juvepoland.com/images/avatars/"+comment.getUser().getAvatarSource(), thumb_image);
            }
        }
        else{
        	thumb_image.setVisibility(LinearLayout.GONE);
        	
        }
        
        
        
        return vi;
	}

}
